package com.ywl.task.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ywl.task.mapper.ProjectMapper;
import com.ywl.task.pojo.Project;
import com.ywl.task.service.ProjectService;
import com.ywl.task.util.Constant;
import com.ywl.task.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 严伟伦
 * @Company 无
 * @Description
 * @Date: 2024/9/5 19:50
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public QdResult queryByPage(Project project, Integer page, Integer limit) {
        // 开启分页  参数1： 页码  参数2： 每页大小,这一条下面第一个方法就是分页方法
        PageHelper.startPage(page,limit);
        // 查询全部
        List<Project> projects = projectMapper.selectByPage(project);

        for (Project project1:projects){
            project1.setCountTasks(projectMapper.countTasks(project1.getProjectId()));
        }

        // 创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<Project> projectPageInfo = new PageInfo<>(projects);

        return QdResult.success(projectPageInfo);
    }



    @Override
    public QdResult selectDeletes(Project project, Integer page, Integer limit) {
        // 开启分页  参数1： 页码  参数2： 每页大小,这一条下面第一个方法就是分页方法
        PageHelper.startPage(page,limit);
        // 查询全部
        List<Project> projects = projectMapper.selectDeletes(project);


        // 创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<Project> projectPageInfo = new PageInfo<>(projects);

        return QdResult.success(projectPageInfo);
    }

    @Override
    public QdResult addProject(Project project) {
        if (project.getStartTime().after(project.getEndTime())){
            return QdResult.error(500,"时间异常");
        }
        if (projectMapper.selectProjectById(project.getProjectId()) != null){
            return QdResult.error(500,"重复");
        }
        if (projectMapper.addProject(project) == 1){
            return QdResult.success(project);
        }
        return QdResult.error();
    }

    @Override
    public QdResult selectProjectById(String projectId) {

        Project projects = projectMapper.selectProjectById(projectId);

        return QdResult.success(projects);
    }

    @Override
    public QdResult updateProjectById(Project project) {
        if (project == null){
            return QdResult.error();
        }
        if (projectMapper.updateProjectById(project) == 1){
            return QdResult.success();
        }
        return QdResult.error();
    }

    @Override
    public QdResult deleteProject(String projectId) {
        if (projectMapper.deleteProject(projectId) == 1){
            return QdResult.success();
        }
        return QdResult.error();
    }

    @Override
    public QdResult updateFile(String projectId) {
        if (projectMapper.updateFile(projectId) == 1){
            return QdResult.success();
        }
        return QdResult.error();
    }


    @Override
    public QdResult updateFileRestore(String projectId) {
        if (projectMapper.updateFileRestore(projectId) == 1){
            return QdResult.success();
        }
        return QdResult.error();
    }

    @Override
    public QdResult updateReleases(String[] projectIds) {
        return QdResult.success(200,"ok",projectMapper.updateProjectsStatus(projectIds,2));
    }

    @Override
    public QdResult updateFiles(String[] projectIds) {
        return QdResult.success(200,"ok",projectMapper.updateProjectsStatus(projectIds,3));
    }


    @Override
    public QdResult updateDeletes(String[] projectIds) {
        return QdResult.success(200,"ok",projectMapper.updateProjectsStatus(projectIds,99));
    }




}
